import api from '@/api'
import { User } from '@/types/api'
import { Button, Form, Input, Select, Space, Table } from 'antd'
import { ColumnsType, ColumnType } from 'antd/es/table'
import { useEffect, useState } from 'react'

function UserList(): JSX.Element {
  const [data, setData] = useState<User.UserItem[]>([])

  const getUserList = async () => {
    const dataTemp = await api.getUserList({ pageNum: 1, pageSize: 999 })
    setData(dataTemp.list)
  }
  useEffect(() => {
    getUserList()
  }, [])

  const columns: ColumnsType<User.UserItem> = [
    {
      key: 'userId',
      title: '用户ID',
      dataIndex: 'userId'
    },
    {
      key: 'userName',
      title: '用户名',
      dataIndex: 'userName'
    },
    {
      key: 'userEmail',
      title: '用户邮箱',
      dataIndex: 'userEmail'
    },
    {
      key: 'role',
      title: '用户角色',
      dataIndex: 'role',
      render(role:number){
        return {
          0:'超级管理员',
          1:'管理员',
          2:'体验管理员',
        }[role]
      }
    },
    {
      key: 'state',
      title: '用户状态',
      dataIndex: 'state',
      render(state:number){
        return {
          0:'在职',
          1:'离职',
          2:'试用期',
        }[state]
      }
    },
    {
      key: 'createTime',
      title: '注册时间',
      dataIndex: 'createTime'
    },
    {
      key: 'action',
      title: '操作',
      dataIndex: 'action',
      render(){
        return <Space>
          <Button type='text'>编辑</Button>
          <Button type='text' danger>删除</Button>
        </Space>
      }
    }
  ]

  return (
    <div className='user-list'>
      <Form className='search-form' layout='inline' initialValues={{ state: 0 }}>
        <Form.Item name='userId' label='用户ID'>
          <Input placeholder='请输入用户id' />
        </Form.Item>
        <Form.Item name='userName' label='用户名称'>
          <Input placeholder='请输入用户名称' />
        </Form.Item>

        <Form.Item name='state' label='状态'>
          <Select
            style={{ width: 120 }}
            options={[
              { value: 0, label: '全部' },
              { value: 1, label: '在职' },
              { value: 2, label: '离职' },
              { value: 3, label: '试用期' }
            ]}
          />
        </Form.Item>
        <Form.Item>
          <Space>
            <Button type='primary'>搜索</Button>
            <Button>重置</Button>
          </Space>
        </Form.Item>
      </Form>
      <div className='base-table'>
        <div className='header-wrapper'>
          <div className='title'>用户列表</div>
          <div className='action'>
            <Space>
              <Button type='primary'>新增</Button>
              <Button type='primary' danger>
                批量删除
              </Button>
            </Space>
          </div>
        </div>

        <Table dataSource={data} columns={columns} />
      </div>
    </div>
  )
}

export default UserList
